/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dda;

import processing.core.*;

/**
 *
 * @author DAVINCI
 */
public class Dda extends PApplet {

    int xStart;
    int yStart;
    int xEnd;
    int yEnd;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        PApplet.main(new String[]{"dda.Dda"});
    }

    @Override
    public void setup() {
        size(800, 600);
        background(200);

    }

    @Override
    public void draw() {
    }

    public void DDA(int xStart, int yStart, int xEnd, int yEnd) {
        int dx = xEnd - xStart, dy = yEnd - yStart;
        float x = xStart, y = yStart;

        int absX = abs(dx), absY = abs(dy);
        float m = (float) dy / (float) dx;

        if (abs(dx) > abs(dy)) {
            //octante 1 y 8
            if (xStart < xEnd) {
                for (int i = 0; i <= absX; i++) {
                    point(x, round(y));
                    x += 1;
                    y += m;
                }
                //octante 4 y 5
            } else {
                for (int i = 0; i <= absX; i++) {
                    point(x, round(y));
                    x -= 1;
                    y -= m;

                }
            }
        } else {
            //octante 2 y 3
            if (yStart < yEnd) {
                for (int i = 0; i <= absY; i++) {
                    x += 1 / m;
                    y += 1;
                    point(round(x), y);
                }
                //octante 6 y 7
            } else {
                for (int i = 0; i <= absY; i++) {
                    x -= 1 / m;
                    y -= 1;
                    point(round(x), y);
                }
            }
        }


    }

    @Override
    public void mousePressed() {
        xStart = mouseX;
        yStart = mouseY;

    }

    @Override
    public void mouseDragged() {
        background(200);
        xEnd = mouseX;
        yEnd = mouseY;
        DDA(xStart, yStart, xEnd, yEnd);
    }
}
